package com.qitianyong.selfclass;

import android.app.Activity;
import android.content.pm.PackageManager;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.LayoutRes;
import android.text.TextUtils;
import android.util.Log;
import android.widget.Toast;

import com.qitianyong.sgkusbcamera.PreviewActivity;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;


// 此类中需处理事情如下：
// 前后台判断标准

public class BaseActivity extends Activity {

    private int __flag = -1;
    protected static Toast __toast;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public void setContentView(@LayoutRes int layoutResID) {
        super.setContentView(layoutResID);
        EventBus.getDefault().register(this);
        if (GKDevice.__hDevice == 0 && !TextUtils.equals(this.getClass().getName(), PreviewActivity.class.getName())) {
            finish();
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        __toast = null;
        EventBus.getDefault().unregister(this);
    }

    public void notifyEmptyBusMessage(int bus_msg) {

        Message2EventBus msg = new Message2EventBus();
        msg.__what = bus_msg;
        EventBus.getDefault().post(msg);
    }

    public void notifyBusMessage(int bus_msg, Object object) {

        Message2EventBus msg = new Message2EventBus();
        msg.__what = bus_msg;
        msg.object = object;
        EventBus.getDefault().post(msg);
    }

    @Subscribe
    public void onEventMainThread(BaseActivityEvent event) {
        switch (event.event) {
            case BaseActivityEvent.DETACHUSB:
                if (!TextUtils.equals(this.getClass().getName(), PreviewActivity.class.getName())) {
                    finish();
                } else {
                    notifyEmptyBusMessage(BUS_MESSAGE.BUS_OFFLINE);
                }
                break;
            case BaseActivityEvent.OPENSTREAM:
                if (TextUtils.equals(this.getClass().getName(), PreviewActivity.class.getName())) {
                    GKDevice.getInstance().openStream();
                }
                break;
        }
    }

    public void requestPerm(final String strPermission, int permission_id) {

        if (!checkPermission(strPermission)) {
            requestPermission(strPermission, permission_id);
        }
    }

    // 检查权限
    private boolean checkPermission(final String strPermission) {
        int permission_id = 0;
        if (Build.VERSION.SDK_INT >= 23) {
            permission_id = checkSelfPermission(strPermission);

            if (permission_id != PackageManager.PERMISSION_GRANTED) {
                return false;
            }
        }

        return true;
    }

    // 请求权限
    private void requestPermission(final String strPermission, int per_id) {

        if (Build.VERSION.SDK_INT >= 23) {
            requestPermissions(new String[]{strPermission}, per_id);
        }
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);

        if (CONSTANT.PERMISSION_WRITE_EXTERNAL_STORAGE == requestCode) {
            if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                __flag = 1;
            } else {
                __flag = 0;
            }
        }

        Log.i("result", "--" + __flag);
    }
}
